package com.pkk.penguin.components.starter.validator.configuration;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

/**
 * @author luffy
 * @description: 参数验证快速返回
 * @date 2019-04-17 16:31
 */
public class HibernateValidateConfiguration {

  @Bean
  public Validator validator() {
    //failFast：true  快速失败返回模式(只要有一个验证失败，则返回)    false 普通模式(会校验完所有的属性，然后返回所有的验证失败信息)
    ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
        .configure()
        .failFast(true)
        .addProperty("hibernate.validator.fail_fast", "true")
        .buildValidatorFactory();
    return validatorFactory.getValidator();
  }

  @Bean
  public MethodValidationPostProcessor methodValidationPostProcessor() {
    MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor();
    //设置validator模式为快速失败返回
    postProcessor.setValidator(validator());
    return postProcessor;
  }
}
